home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / totsrc.zip / TOTMISC.PAS < prev    next >
Pascal/Delphi Source File  |  1991-02-11  |  15KB  |  611 lines

  1. {               Copyright 1991 TechnoJock Software, Inc.               }
  2. {                          All Rights Reserved                         }
  3. {                         Restricted by License                        }
  4.  
  5. {                             Build # 1.00                             }
  6.  
  7. Unit totMISC;
  8. {$I TOTFLAGS.INC}
  9.  
  10. {
  11.  Development Notes:
  12.  
  13. }
  14.  
  15. INTERFACE
  16.  
  17. Uses DOS, CRT, totSTR, totFAST;
  18.  
  19. var
  20.   LPTport:byte;     {0=lpt1, 1=lpt2, 2=lpt3}
  21.  
  22. procedure Swap(var A,B:longint);
  23. function  WithinRange(Min,Max,Test: longint): boolean;
  24. function  Exist(Filename:string):boolean;
  25. function  CopyFile(SourceFile, TargetFile:string): shortint;
  26. function  DeleteFile(Filename:string): shortint;
  27. function  RenameFile(Oldname,NewName:string):shortint;
  28. function  FSize(Filename:string): longint;
  29. function  FileDrive(Full:string): string;
  30. function  FileDirectory(Full:string): string;
  31. function  FileName(Full:string): string;
  32. function  FileExt(Full:string): string;
  33. function  SlashedDirectory(Dir:string):string;
  34. function  PrinterStatus:byte;
  35. function  AlternatePrinterStatus:byte;
  36. function  PrinterReady :boolean;
  37. procedure ResetPrinter;
  38. procedure PrintScreen;
  39. procedure Beep;
  40. function  CurrentTime: string;
  41. function  ParamLine: String;
  42. function  ParamVal(Marker:string): string;
  43. function  Frequency(Match:string;Source:string): byte;
  44. function  ValidFileName(FN:string): shortint;
  45. procedure ResetStartUpMode;
  46. function  RunAnything(Command: string):integer;
  47. function  RunEXECOM(Progname, Params: string):integer;
  48. function  RunDOS(Msg:string):integer;
  49.  
  50. IMPLEMENTATION
  51. VAR
  52.     StartTop,      {used to record initial screen state when program is run}
  53.     StartBot   : Byte;
  54.     StartMode  : word;
  55.  
  56. procedure Swap(var A,B:longint);
  57. {}
  58. var Temp: longint;
  59. begin
  60.    Temp := A;
  61.    A := B;
  62.    B := Temp;
  63. end; {Swap}
  64.  
  65. function WithinRange(Min,Max,Test: longint): boolean;
  66. {}
  67. begin
  68.    if Min > Max then
  69.       Swap(Min,Max);
  70.    WithinRange := (Test >= Min) and (Test <= Max);
  71. end; {WithinRange}
  72.  
  73. function Exist(Filename:string):boolean;
  74. {returns true if file exists}
  75. var Inf: SearchRec;
  76. begin
  77.     findfirst(Filename,AnyFile,Inf);
  78.     Exist := (DOSError = 0);
  79. end;  {func Exist}
  80.  
  81. function CopyFile(SourceFile, TargetFile:string): shortint;
  82. {return codes:  0 successful
  83.                 1 source and target the same
  84.                 2 cannot open source
  85.                 3 unable to create target
  86.                 4 error during copy
  87. }
  88. var
  89.   Source,
  90.   Target: file;
  91.   BRead,
  92.   Bwrite: word;
  93.   FileBuf: array[1..2048] of char;
  94. begin
  95.    if SourceFile = TargetFile then
  96.       CopyFile := 1
  97.    else
  98.    begin
  99.       assign(Source,SourceFile);
  100.       {$I-}
  101.       reset(Source,1);
  102.       {$I+}
  103.       if IOResult <> 0 then
  104.           CopyFile := 2
  105.       else
  106.       begin
  107.          Assign(Target,TargetFile);
  108.          {$I-}
  109.          Rewrite(Target,1);
  110.          {$I+}
  111.          if IOResult <> 0 then
  112.             CopyFile := 3
  113.          else
  114.          begin
  115.             repeat
  116.               blockread(Source,FileBuf,SizeOf(FileBuf),BRead);
  117.               blockwrite(Target,FileBuf,Bread,Bwrite);
  118.             until (Bread = 0) or (Bread <> BWrite);
  119.             close(Source);
  120.             close(Target);
  121.             if Bread <> Bwrite then
  122.                CopyFile := 4
  123.             else
  124.                CopyFile := 0;
  125.          end;
  126.       end;
  127.    end;
  128. end; {CopyFile}
  129.  
  130. function FSize(Filename:string): longint;
  131. {returns  -1   if file not found}
  132. var
  133.    F : file of byte;
  134. begin
  135.     Assign(F,Filename);
  136.     {$I-}
  137.     Reset(F);
  138.     {$I+}
  139.     if IOResult <> 0 then
  140.     begin
  141.        FSize := -1;
  142.        exit;
  143.     end;
  144.     FSize := FileSize(F);
  145.     Close(F);
  146. end; {FSize}
  147.  
  148. function FileSplit(Part:byte;Full:string): string;
  149. {used internally}
  150. var
  151.    D : DirStr;
  152.    N : NameStr;
  153.    E : ExtStr;
  154. begin
  155.    FSplit(Full,D,N,E);
  156.    Case Part of
  157.    1 : FileSplit := D;
  158.    2 : FileSplit := N;
  159.    3 : FileSplit := E;
  160.    end;
  161. end; {FileSplit}
  162.  
  163. function FileDrive(Full:string): string;
  164. {}
  165. var
  166.   Temp : string;
  167.   P : byte;
  168. begin
  169.    Temp := FileSplit(1,Full);
  170.    P := Pos(':',Temp);
  171.    if P <> 2 then
  172.       FileDrive := ''
  173.    else
  174.       FileDrive := upcase(Temp[1]);
  175. end; {FileDrive}
  176.  
  177. function FileDirectory(Full:string): string;
  178. {}
  179. var
  180.   Temp : string;
  181.   P : byte;
  182. begin
  183.    Temp := FileSplit(1,Full);
  184.    P := Pos(':',Temp);
  185.    if P = 2 then
  186.       Delete(Temp,1,2);                 {remove drive}
  187.    if (Temp[length(Temp)]  ='\') and (temp <> '\') then
  188.       Delete(temp,length(Temp),1);      {remove last backslash}
  189.    FileDirectory := Temp;
  190. end; {FileDirectory}
  191.  
  192. function FileName(Full:string): string;
  193. {}
  194. begin
  195.    FileName := FileSplit(2,Full);
  196. end; {FileName}
  197.  
  198. function FileExt(Full:string): string;
  199. {}
  200. var
  201.   Temp : string;
  202. begin
  203.    Temp := FileSplit(3,Full);
  204.    if (Temp = '') or (Temp = '.') then
  205.       FileExt := temp
  206.    else
  207.       FileExt := copy(Temp,2,3);
  208. end; {FileExt}
  209.  
  210. function SlashedDirectory(Dir:string):string;
  211. {}
  212. begin
  213.    if (Dir = '') or (Dir[length(Dir)] in [':','\']) then
  214.       SlashedDirectory := Dir
  215.    else
  216.       SlashedDirectory := Dir + '\';
  217. end; {SlashedDirectory}
  218.  
  219. function PrinterStatus:byte;
  220. {Credits: Robert W. Lewis, VA thanks! Special masking employed for non-
  221.           standard printers, e.g. daisy wheels!!! }
  222. var Recpack : registers;
  223. begin
  224.    with Recpack do
  225.    begin
  226.       Ah := 2;
  227.       Dx := LPTport;
  228.       intr($17,recpack);
  229.       if (Ah and $B8) = $90 then
  230.          PrinterStatus := 0        {all's well}
  231.       else if (Ah and $20) = $20 then
  232.          PrinterStatus := 1        {no Paper}
  233.       else if (Ah and $10) = $00 then
  234.          PrinterStatus := 2        {off line}
  235.       else if (Ah and $80) = $00 then
  236.          PrinterStatus := 3        {busy}
  237.       else if (Ah and $08) = $08 then
  238.          PrinterStatus := 4;       {undetermined error}
  239.    end;
  240. end; {PrinterStatus}
  241.  
  242. function AlternatePrinterStatus:byte;
  243. var Recpack : registers;
  244. begin
  245.    with recpack do
  246.    begin
  247.       Ah := 2;
  248.       Dx := LPTport;
  249.       intr($17,recpack);
  250.       if (Ah and $20) = $20 then
  251.             AlternatePrinterStatus := 1  {no Paper}
  252.       else if (Ah and $10) = $00 then
  253.             AlternatePrinterStatus := 2  {off line}
  254.       else if (Ah and $80) = $00 then
  255.             AlternatePrinterStatus := 3  {busy}
  256.       else if (Ah and $08) = $08 then
  257.             AlternatePrinterStatus := 4  {undetermined error}
  258.       else
  259.           AlternatePrinterStatus := 0    {all's well}
  260.    end;
  261. end; {AlternatePrinterStatus}
  262.  
  263. function PrinterReady :boolean;
  264. begin
  265.     PrinterReady := (PrinterStatus = 0);
  266. end; {PrinterReady}
  267.  
  268. procedure ResetPrinter;
  269. var 
  270.   address: integer absolute $0040:$0008;
  271.   portno,delay : integer;
  272. begin
  273.    portno := address + 2;
  274.    port[portno] := 232;
  275.    for delay := 1 to 2000 do {nothing};
  276.    port[portno] := 236;
  277. end; {ResetPrinter}
  278.  
  279. function CurrentTime: string;
  280. var
  281.   hour,min,sec:     string[2];
  282.   H,M,S,T : word;
  283. begin
  284.   GetTime(H,M,S,T);
  285.   Str(H,Hour);
  286.   Str(M,Min);
  287.   Str(S,Sec);
  288.   if S < 10 then        {pad a leading zero if sec is < 10 }
  289.      sec := '0'+sec;
  290.   if M < 10 then        {pad a leading zero if min is < 10 }
  291.      min := '0'+min;
  292.   if H > 12 then        { assign an a.m. or p.m. string }
  293.   begin
  294.      str(H - 12,hour);
  295.      if length(hour) = 1 then Hour := ' '+hour;
  296.      CurrentTime := hour+':'+min+':'+sec+' p.m.'
  297.   end
  298.   else if H < 1 then
  299.      CurrentTime := '12'+':'+min+':'+sec+' a.m.'
  300.   else
  301.      CurrentTime := hour+':'+min+':'+sec+' a.m.';
  302. end; {CurrentTime}
  303.  
  304. procedure PrintScreen;
  305. var Regpack : registers;
  306. begin
  307.    intr($05,regpack);
  308. end; {PrintScreen}
  309.  
  310. procedure Beep;
  311. begin
  312.     sound(800);Delay(150);
  313.     sound(600);Delay(100);
  314.     Nosound;
  315. end; {Beep}
  316.  
  317. function ParamLine: String;
  318. {returns the command line as a space delimited string}
  319. var 
  320.  I : integer;
  321.  P : integer;
  322.  Line : string;
  323. begin
  324.    Line := '';
  325.    P := ParamCount;
  326.    if P > 0 then
  327.       for I := 1 to P do
  328.           Line := Line + ParamStr(I) + ' ';
  329.    ParamLine := Line;
  330. end; {ParamLine}
  331.  
  332. function ParamVal(Marker:string): string;
  333. {searches for Marker in string and returns the characters following}
  334. var
  335.    ValStr,
  336.    Line : string;
  337.    Loc1, Loc2 : integer;
  338. begin
  339.    Line := ParamLine;
  340.    ValStr := '';
  341.    if Line <> '' then
  342.    begin
  343.       Loc1 := pos(SetUpper(Marker),SetUpper(Line));
  344.       if Loc1 = 0 then {not found}
  345.          ValStr := ''
  346.       else
  347.       begin
  348.          Loc1 := Loc1 + length(Marker);
  349.          if (Loc1 > Length(Line)) 
  350.          or (Line[Loc1] = Marker[1]) then
  351.             ValStr := ''
  352.          else
  353.          begin
  354.             Loc2 := Loc1;
  355.             repeat
  356.                inc(Loc2)
  357.             until (Line[Loc2] = Marker[1])
  358.                or (Loc2 > length(Line));
  359.             ValStr := Copy(Line,Loc1,Loc2-Loc1);
  360.          end;
  361.       end;
  362.    end;
  363.    ParamVal := ValStr;
  364. end; {ParamVal}
  365.  
  366. function Frequency(Match:string;Source:string): byte;
  367. {returns the number of times that Match occurs in SOURCE}
  368. var
  369.   Len,Loc, Counter : byte;
  370. begin
  371.    Counter := 0;
  372.    Len := Length(match);
  373.    if (Len <> 0) and (length(Source) > 0) then
  374.       repeat 
  375.          Loc := pos(Match,Source);
  376.          if Loc <> 0 then
  377.          begin
  378.             inc(Counter);
  379.             delete(Source,Loc,length(Match));
  380.          end;
  381.       until Loc = 0;
  382.    Frequency := Counter;
  383. end; {Frequency}
  384.  
  385. function ValidFileName(FN:string): shortint;
  386. {Validates a file path and name and returns following
  387.  codes:
  388.           -1     Path and name OK but file does not exist
  389.            0     Path and name OK and file exists
  390.            1     Illegal drive specifier
  391.            2     Illegal characters in path
  392.            3     Invalid Path
  393.            4     No file specified
  394.            5     Illegal Characters in name
  395.            6     Name longer than eight characters
  396.            7     Extension longer than three characters
  397. }
  398. const
  399.    Illegal:string[16] = ' +=/[]":;,?*<>|.';
  400. var
  401.    ECode: shortint;
  402.    OldDIR,D,P,F,E: string;
  403.    Loc: byte;
  404.  
  405.    function Legal(Str:string;AllowSlash:boolean): boolean;
  406.    {}
  407.    var I : integer;
  408.    begin
  409.       Legal := true;
  410.       for I := 1 to 16 do
  411.          if pos(Illegal[I],Str) <> 0 then
  412.          begin
  413.             Legal := false;
  414.             exit;
  415.          end;
  416.       if not AllowSlash then
  417.          if pos('\',Str) > 0 then
  418.             legal := false;
  419.    end;
  420.  
  421. begin
  422.    ECode := 0;
  423.    Loc := pos(':',FN);
  424.    if Loc = 0 then
  425.    begin
  426.       D := '';
  427.       P := FN;
  428.    end
  429.    else
  430.    begin
  431.       D := SetUpper(copy(FN,1,Loc));
  432.       P := copy(FN,succ(Loc),255);
  433.       if (Loc <> 2) or ((D[1] in ['A'..'Z'])=false) then
  434.       begin
  435.          ValidFileName := 1;
  436.          exit;
  437.       end;
  438.    end;
  439.    Loc := LastPos('\',P);
  440.    if Loc = 0 then
  441.    begin
  442.       F := P;
  443.       P := '';
  444.    end
  445.    else
  446.    begin
  447.       F := copy(P,succ(Loc),255);
  448.       P := copy(P,1,pred(Loc));
  449.    end;
  450.    Loc := pos('.',F);
  451.    if Loc = 0 then
  452.       E := ''
  453.    else
  454.    begin
  455.       E := copy(F,succ(Loc),255);
  456.       F := copy(F,1,pred(Loc));
  457.    end;
  458.    if not legal(P,true) then
  459.       Ecode := 2
  460.    else
  461.    begin
  462.       if D+P <> '' then
  463.       begin
  464.          GetDir(0,OldDir);
  465.          {$I-}
  466.          ChDir(D+P);
  467.          {$I+}
  468.          if IOResult <> 0 then
  469.          begin
  470.             ValidFileName := 3;
  471.             exit;
  472.          end
  473.          else
  474.             ChDir(OldDir);
  475.       end;
  476.       if (F='') and (E='') then
  477.          Ecode := 4
  478.       else
  479.       begin
  480.          if not Legal(F+E,false) then
  481.             Ecode := 5
  482.          else
  483.          begin
  484.             if length(F) > 8 then
  485.                Ecode := 6
  486.             else if length(E) > 3 then
  487.                Ecode := 7;
  488.          end;
  489.       end;
  490.    end;
  491.    if Ecode = 0 then
  492.       if not Exist(FN) then
  493.          ECode := -1;
  494.    ValidFileName := Ecode;
  495. end; {ValidFileName}
  496.  
  497. function DeleteFile(Filename:string): shortint;
  498. {Return codes:   -1    File not found
  499.                   0    File deleted
  500.                   1    Error - file not deleted.
  501.  
  502. }
  503. var F: file;
  504. begin
  505.    if not Exist(Filename) then
  506.       DeleteFile := -1
  507.    else
  508.    begin
  509.       assign(F,Filename);
  510.       {$I-}
  511.       Erase(F);
  512.       {$I+}
  513.       if ioresult = 0 then
  514.          DeleteFile := 0
  515.       else
  516.          DeleteFile := 1;
  517.    end;
  518. end; {DeleteFile}
  519.  
  520. function RenameFile(Oldname,NewName:string):shortint;
  521. {Retcodes:     0 file renamed
  522.                1 file not found
  523.                2 rename failed
  524. }
  525. var F:file;
  526. begin
  527.    if not exist(OldName) then
  528.       RenameFile := 1
  529.    else
  530.    begin
  531.       assign(F,Oldname);
  532.       {$I-}
  533.       Rename(F,Newname);
  534.       {$I+}
  535.       if ioresult = 0 then
  536.          RenameFile := 0
  537.       else
  538.          RenameFile := 2;
  539.    end;
  540. end; {RenameFile}
  541.  
  542. procedure ResetStartUpMode;
  543. {resets monitor mode and cursor settings to the state they
  544.  were in at program startup}
  545. begin
  546.    TextMode(StartMode);
  547.    Screen.CursSize(StartTop,StartBot);
  548. end; {ResetStartUpMode}
  549.  
  550. {IMPORTANT NOTE: You must use the $M compiler directive to instruct Turbo
  551. Pascal to leave some memory for the spawned or child program, e.g. 
  552. $M $8192,$8192,$8192. The precise values depend on the size of your program
  553. ..experiment. If the child process runs OK, try smaller values.}
  554.  
  555. function RunEXECOM(Progname, Params: string): integer;
  556. {}
  557. begin
  558.    swapvectors;
  559.    exec(Progname,Params);
  560.    swapvectors;
  561.    RunEXECOM := doserror;
  562. end; {RunEXECOM}
  563.  
  564. function RunAnything(command: string):integer;
  565. {}
  566. var Comspec:string;
  567. begin
  568.    Comspec := GetEnv('COMSPEC');
  569.    swapvectors;
  570.    exec(comspec,'/C '+command);
  571.    SwapVectors;
  572.    RunAnything := doserror;
  573. end; {RunAnything}
  574.  
  575. function RunDOS(Msg:string):integer;
  576. {}
  577. var Comspec:string;
  578. begin
  579.    Comspec := GetEnv('COMSPEC');
  580.    swapvectors;
  581.    writeln;
  582.    writeln(Msg);
  583.    exec(comspec,'');
  584.    SwapVectors;
  585.    RunDOS := doserror;
  586. end; {RunDOS}
  587. {|||||||||||||||||||||||||||||||||||||||||||||||}
  588. {                                               }
  589. {     U N I T   I N I T I A L I Z A T I O N     }
  590. {                                               }
  591. {|||||||||||||||||||||||||||||||||||||||||||||||}
  592. procedure MiscInit;
  593. {initilizes objects and global variables}
  594. begin
  595.    LPTport := 0;  {LPT1}
  596.    StartMode := LastMode; {record the initial state of screen when program was executed}
  597.    Screen.CursSave;
  598.    StartTop := Screen.CursTop;
  599.    StartBot := Screen.CursBot;
  600. end; {MiscInit}
  601.  
  602. {end of unit - add initialization routines below}
  603. {$IFNDEF OVERLAY}
  604. begin
  605.    MiscInit;
  606. {$ENDif}
  607. end.
  608.  
  609.  
  610.  
  611.